
#include <QColor>
#include <QFont>
#include <QPainter>
#include <QPaintEvent>
#include <QPoint>
#include <QResizeEvent>
#include <QVBoxLayout>

#include "ui/button.h"
#include "ui/home.h"

Home::Home(QWidget* pkParent)
    : QWidget(pkParent),
      m_ratio(0.75),
      m_kTitle(":/image/text/title.png"),
      m_kBackground(":/image/board/board.png"),
      m_pkStartButton(new Button(":/image/button/normal_start.png", this))
{
    // Check.
    Q_CHECK_PTR(m_pkStartButton);
    Q_ASSERT(!m_kBackground.isNull());

    // Setup widget.
    setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    setLayout(new QVBoxLayout(this));
    layout()->setEnabled(false);
    layout()->addWidget(m_pkStartButton);

    // Connections.
    connect(m_pkStartButton, SIGNAL(clicked()), this, SIGNAL(start()));
}

Home::~Home()
{
}

QSize Home::minimumSizeHint() const
{
    return sizeHint() / 2;
}

QSize Home::sizeHint() const
{
    return m_kBackground.size();
}

void Home::paintEvent(QPaintEvent* pkEvent)
{
    // Propagate.
    QWidget::paintEvent(pkEvent);

    // Base scaling on background pixmap.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());

    // Paint.
    QPainter kPainter(this);

    // Paint the background.
    QSize kScaledBackgroundSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kBackgroundOffset((width() - kScaledBackgroundSize.width()) / 2, (height() - kScaledBackgroundSize.height()) / 2 - kScaledBackgroundSize.height() / 8);

    kPainter.drawPixmap(QRectF(kBackgroundOffset.x(), kBackgroundOffset.y(), kScaledBackgroundSize.width(), kScaledBackgroundSize.height()), m_kBackground, m_kBackground.rect());

    // Paint the title.
    QSize kScaledTitleSize(m_kTitle.width() * limitingRatio, m_kTitle.height() * limitingRatio);
    QPoint kTitleOffset((width() - kScaledTitleSize.width()) / 2, (height() - kScaledTitleSize.height()) / 2 - kScaledTitleSize.height() * 1.2);

    kPainter.drawPixmap(QRectF(kTitleOffset.x(), kTitleOffset.y(), m_kTitle.width() * limitingRatio, m_kTitle.height() * limitingRatio), m_kTitle, m_kTitle.rect());

    // Paint the text.
    kPainter.setFont(QFont("Wortellina", kScaledBackgroundSize.height() / 30));

    kPainter.drawText(kTitleOffset.x(), kTitleOffset.y() + kScaledTitleSize.height(), kScaledTitleSize.width(), kScaledTitleSize.height(), Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextWordWrap, trUtf8("Credits :\nGoudard Adrian - Gauthier Gelis\nYacine Izza - Anas Kharboutly\nDavid Carmona - Gabriel Goudard"));
}

void Home::resizeEvent(QResizeEvent* pkEvent)
{
    // Propagate.
    QWidget::resizeEvent(pkEvent);

    // Compute scaled background coordinates.
    qreal limitingRatio = qMin(width() * m_ratio / m_kBackground.width(),
                               height() * m_ratio / m_kBackground.height());
    QSize kScaledSize(m_kBackground.width() * limitingRatio, m_kBackground.height() * limitingRatio);
    QPoint kOffset((width() - kScaledSize.width()) / 2, (height() - kScaledSize.height()) / 2 - kScaledSize.height() / 8);

    // Button replace.
    int iWidth = m_pkStartButton->sizeHint().width() * limitingRatio;
    int iHeight = m_pkStartButton->sizeHint().height() * limitingRatio;
    int iX = (width() - iWidth) / 2;
    int iY = kOffset.y() + kScaledSize.height() - iHeight / 2;
    m_pkStartButton->setGeometry(iX, iY, iWidth, iHeight);
}
